package com.tie.controller;

import com.tie.entity.RegisterEntity;
import com.tie.entity.ResponseEntity;
import com.tie.entity.User;
import com.tie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/checkLogin")
    @ResponseBody
    public ResponseEntity checkLogin(@RequestParam(value = "phone") String phone) {

        ResponseEntity responseEntity = new ResponseEntity();

        //校验手机号
        boolean matches = phone.matches(PHONE_NUMBER_REG);
        if (!matches) {
            responseEntity.setCode("500");
            responseEntity.setMessage("手机号不合法");
            return responseEntity;
        }

        User user = userService.findByPhone(phone);

        if (user != null) {
            responseEntity.setCode("200");
            responseEntity.setMessage("请求成功");
            responseEntity.setSuccessed(true);
            return responseEntity;
        }else{
            responseEntity.setCode("200");
            responseEntity.setMessage("请求成功");
            responseEntity.setSuccessed(false);
            return responseEntity;
        }
    }


    @RequestMapping(value = "/register")
    @ResponseBody
    public RegisterEntity register(@RequestParam(value = "phone") String phone,@RequestParam(value = "password") String password) {
        String guid = getGuid();

        RegisterEntity registerEntity = new RegisterEntity();

        //校验手机号
        boolean matches = phone.matches(PHONE_NUMBER_REG);
        if (!matches) {
            registerEntity.setCode("500");
            registerEntity.setMessage("手机号不合法");
            return registerEntity;
        }

        //校验密码位数
        boolean passwordMatches = password.matches(PASSWORD_NUMBER_REG);
        if (!passwordMatches) {
            registerEntity.setCode("500");
            registerEntity.setMessage("密码长度不正确");
            return registerEntity;
        }

        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setCustomerId(guid);
        try {
            userService.save(user);
        } catch (Exception e) {
            registerEntity.setCode("500");
            registerEntity.setMessage("注册失败");
            return registerEntity;
        }


        registerEntity.setCode("200");
        registerEntity.setMessage("注册成功");
        registerEntity.setCustomerId(guid);
        registerEntity.setPhone(phone);

        return registerEntity;
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public RegisterEntity login(@RequestParam(value = "phone") String phone,@RequestParam(value = "password") String password) {

        RegisterEntity registerEntity = new RegisterEntity();

        //校验手机号
        boolean matches = phone.matches(PHONE_NUMBER_REG);
        if (!matches) {
            registerEntity.setCode("500");
            registerEntity.setMessage("手机号不合法");
            return registerEntity;
        }

        //校验密码位数
        boolean passwordMatches = password.matches(PASSWORD_NUMBER_REG);
        if (!passwordMatches) {
            registerEntity.setCode("500");
            registerEntity.setMessage("密码长度不正确");
            return registerEntity;
        }

        User user = userService.findUserByPassword(phone, password);
        
        if (user != null) {
            registerEntity.setCode("200");
            registerEntity.setMessage("登录成功");
            registerEntity.setCustomerId(user.getCustomerId());
            registerEntity.setPhone(phone);
            return registerEntity;
        }else {
            registerEntity.setCode("500");
            registerEntity.setMessage("登录失败");
            return registerEntity;
        }

    }

    /**
     * 20位末尾的数字id
     */
    public static int Guid=100;

    public static String getGuid() {

        Guid+=1;

        long now = System.currentTimeMillis();
        //获取4位年份数字
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");
        //获取时间戳
        String time=dateFormat.format(now);
        String info=now+"";
        //获取三位随机数
        //int ran=(int) ((Math.random()*9+1)*100);
        //要是一段时间内的数据连过大会有重复的情况，所以做以下修改
        int ran=0;
        if(Guid>999){
            Guid=100;
        }
        ran=Guid;

        return time+info.substring(2, info.length())+ran;
    }

    /**
     * 手机号码正则表达式
     */
    private static final String PHONE_NUMBER_REG = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";

    /**
     * 密码6-13位
     */
    private static final String PASSWORD_NUMBER_REG = "^\\d{6,13}$";
}
